<?php
/**
 * The model for grid columns
 *
 *
 * @author     Moldovan Calin <mcalin45@gmail.com>
 * @author     Vlad Razvan <razvanvlad1982@yahoo.com>
 * @author     Jipa Ciprian <ciprian.jipa@yahoo.com>
 * @copyright  Copyright (c) 2012
 * @version    $Id: User.php 43 2012-09-27 07:14:48Z mcalin45@gmail.com $
 */

namespace Crc\Grid;

/**
 * Class for columns.
 *
 * @author     Moldovan Calin <mcalin45@gmail.com>
 * @author     Vlad Razvan <razvanvlad1982@yahoo.com>
 * @author     Jipa Ciprian <ciprian.jipa@yahoo.com>
 * @copyright  Copyright (c) 2012
 */

class Column
{
    /**
     * Column field
     *
     * @var string
     */
    protected $_field;

    /**
     * Column label
     *
     * @var string
     */
    protected $_label;

    /**
     * Daca este true coloana  este afisata in listing
     *
     * @var boolean
     */
    protected $_isVisible = false;

    /**
     *  Daca este true Coloana poate fi afisata  in listing
     * @var boolean
     */
    public $_show = false;

    /**
     *  Daca true este afisat implicit in listing
     * @var boolean
     */
    public $_default = false;

    /**
     * Daca true coloana se poate face  ordonare dupa coloana
     * @var boolean;
     */
    public $_sortable = false;

    /**
     * Ordinea in care sunt afisate coloanele in grid
     * @var integer;
     */
    public $_sortOrder = 0;

    /**
     * Daca  are valoare este coloana dupa care se ordoneaza initial rezultatele unui listing
     * @var boolean;
     */
    public $_order = "";


    public function setField($_field)
    {
        $this->_field = $_field;
        return $this;
    }

    public function getField()
    {
        return $this->_field;
    }

    public function setLabel($_label)
    {
        $this->_label = $_label;
        return $this;
    }

    public function getLabel()
    {
        return $this->_label;
    }

    public function setVisible($_visible = true)
    {
        $this->_isVisible = $_visible;
        return $this;
    }

    public function getVisible()
    {
        return $this->_isVisible;
    }

    public function setShow($_show)
    {
        $this->_show = $_show;
        return $this;
    }

    public function getShow()
    {
        return $this->_show;
    }

    public function setDefault($_default = true)
    {
        $this->_default = $_default;
        return $this;
    }

    public function getDefault()
    {
        return $this->_default;
    }

    public function setSortable($_sortable = true)
    {
        $this->_sortable = $_sortable;
        return $this;
    }

    public function getSortable()
    {
        return $this->_sortable;
    }

    public function setSortOrder($_sortOrder)
    {
        $this->_sortOrder = $_sortOrder;
        return $this;
    }

    public function getSortOrder()
    {
        return $this->_sortOrder;
    }

    public function setOrder($_order)
    {
        $_order = strtoupper($_order);
        if ($_order != 'DESC' && $_order != 'ASC') {
            throw new \Exception("Invalid order type " . $_order . ' on field ' . $this->getField());
        }
        $this->_order = $_order;
        return $this;
    }

    public function getOrder()
    {
        return $this->_order;
    }

    public function isDefaultOrder()
    {
        if (!empty($this->_order)) {
            return true;
        }
        return false;
    }


    public function setFromAnnotation(\Crc\Annotations\Grid\Column $annotation)
    {
        $this->setShow($annotation->show);
        if ($annotation->show) {
            $this->setLabel($annotation->label)
                 ->setDefault($annotation->default)
                 ->setSortable($annotation->sortable);
            if (!empty($annotation->order))  {
                $this->setOrder($annotation->order);
            }
        }
        return $this;
    }
}